package com.jackrain.nea.fc.vp.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.vp.api.ViewCvipinteftpSaveCmd;
import com.jackrain.nea.fc.vp.api.ViewCvipopenCmd;
import com.jackrain.nea.fc.vp.api.ViewCvipopenSaveCmd;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.util.ValueHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;

/**
 * @author xuxinshuai
 * @create 2017/11/13
 */
@Slf4j
@Api(description = "会员接口—VIP开卡-新增/修改/查询")
@RestController
public class ViewCvipopenCtrl {

    @Reference(version = "1.0", group = "vp")
    private ViewCvipopenSaveCmd viewCvipopenSaveCmd;

    @Reference(version = "1.0", group = "vp")
    private ViewCvipopenCmd viewCvipopenCmd;


    @ApiOperation(value = "会员接口—VIP开卡-新增/修改")
    @RequestMapping(path = "fc/api/viewvipopen/save", method = RequestMethod.POST)
    public JSONObject viewVipopenSave(HttpServletRequest request,
                                      @RequestParam(value = "param", required = true) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(1);
            map.put("param", JSON.parseObject(param));
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.vp.api.ViewCvipopenSaveCmd", "vp", "1.0");
            result = ((ViewCvipopenSaveCmd) o).execute(map);
            if (result == null){
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null){
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException){
                result.put("error", "服务错误：" + e.getMessage());
            }else{
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }

        return result.toJSONObject();

    }

    @ApiOperation(value = "会员接口—VIP开卡-查询")
    @RequestMapping(path = "fc/api/viewvipopen/query", method = RequestMethod.POST)
    public JSONObject viewCvipopenSelect(HttpServletRequest request,
                                         @RequestParam(value = "param", required = true) String param) throws Exception {

        ValueHolder result = new ValueHolder();
        try {
            HashMap map = new HashMap(1);
            map.put("param", JSON.parseObject(param));
            Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.fc.vp.api.ViewCvipopenCmd", "vp", "1.0");
            result = ((ViewCvipopenCmd) o).execute(map);
            if (result == null){
                result = new ValueHolder();
                result.put("code", -1);
                result.put("error", "服务错误：返回为空");
            }
            if (result.get("code") == null){
                result.put("code", 0);
            }

        } catch (Exception e) {
            result.put("code", -1);
            if (e instanceof NDSException){
                result.put("error", "服务错误：" + e.getMessage());
            }else{
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                result.put("error", "服务错误：" + sw.toString());
            }
        }

        return result.toJSONObject();

    }

}
